using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The DrivingLicenceTypeTableDB class is responsible for interacting with the database to retrieve and store information
/// about DrivingLicenceTypeTable objects.
/// </summary>
public class DrivingLicenceTypeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of DrivingLicenceTypeTable from the underlying datasource.
/// </summary>
/// <param name="drivingLicenceTypeCode">The unique DrivingLicenceTypeCode of the DrivingLicenceTypeTable in the database.</param>
/// <returns>An DrivingLicenceTypeTable when the DrivingLicenceTypeCode was found in the database, or null otherwise.</returns>
public static DrivingLicenceTypeTable GetItem(short drivingLicenceTypeCode)
{
DrivingLicenceTypeTable myDrivingLicenceTypeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDrivingLicenceTypeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@drivingLicenceTypeCode", drivingLicenceTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myDrivingLicenceTypeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myDrivingLicenceTypeTable;
}
}

/// <summary>
/// Returns a list with DrivingLicenceTypeTable objects.
/// </summary>
/// <returns>A generics List with the DrivingLicenceTypeTable objects.</returns>
public static DrivingLicenceTypeTableList GetList()
{
DrivingLicenceTypeTableList tempList = new DrivingLicenceTypeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDrivingLicenceTypeCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a DrivingLicenceTypeTable in the database.
/// </summary>
/// <param name="myDrivingLicenceTypeTable">The DrivingLicenceTypeTable instance to save.</param>
/// <returns>The new DrivingLicenceTypeCode if the DrivingLicenceTypeTable is new in the database or the existing DrivingLicenceTypeCode when an item was updated.</returns>
public static Int16 Save(DrivingLicenceTypeTable myDrivingLicenceTypeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDrivingLicenceTypeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myDrivingLicenceTypeTable.DrivingLicenceTypeCode == -1)
{myCommand.Parameters.AddWithValue("@drivingLicenceTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@drivingLicenceTypeCode", myDrivingLicenceTypeTable.DrivingLicenceTypeCode);
}
if (myDrivingLicenceTypeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myDrivingLicenceTypeTable.Description);
}
if (myDrivingLicenceTypeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myDrivingLicenceTypeTable.Status);
}
if (myDrivingLicenceTypeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myDrivingLicenceTypeTable.CreatedDateTime);
}
if (myDrivingLicenceTypeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myDrivingLicenceTypeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a DrivingLicenceTypeTable from the database.
/// </summary>
/// <param name="drivingLicenceTypeCode">The DrivingLicenceTypeCode of the DrivingLicenceTypeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(short drivingLicenceTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDrivingLicenceTypeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@drivingLicenceTypeCode", drivingLicenceTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the DrivingLicenceTypeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static DrivingLicenceTypeTable FillDataRecord(IDataRecord myDataRecord )
{
DrivingLicenceTypeTable myDrivingLicenceTypeTable = new DrivingLicenceTypeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DrivingLicenseTypeCode")))
{
myDrivingLicenceTypeTable.DrivingLicenceTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DrivingLicenseTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myDrivingLicenceTypeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myDrivingLicenceTypeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myDrivingLicenceTypeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myDrivingLicenceTypeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myDrivingLicenceTypeTable;
}
}
